import request from '@/config/request'
import { getAnswerScoreable, jywConvertToXkw } from '@/utils/question'
import { getSelfModuleSubjectApi, questionSplitApi } from '@/api/common'

// 获取课程列表
export const getStudentCoursesList = (data) => {
  return new Promise((resolve) => {
    Promise.all([
      request.get('/question-api/question/courses/getStudentCoursesList', data),
      getSelfModuleSubjectApi()
    ]).then((res) => {
      const [subjectIdListData, moduleSubjectListData] = res
      const { data: subjectIdList } = subjectIdListData
      const { data: moduleSubjectList } = moduleSubjectListData

      // 获取已经开通的科目
      if (Array.isArray(moduleSubjectList) && moduleSubjectList.length > 0) {
        const selfSubjectList = moduleSubjectList.filter((i) => i.moduleId === '4')
        if (Array.isArray(subjectIdList) && selfSubjectList.length > 0) {
          const copySubjectIdList = JSON.parse(JSON.stringify(subjectIdList))
          const selfSubjectIds = selfSubjectList.map((i) => i.subject)
          subjectIdList.splice(0, subjectIdList.length)
          subjectIdList.push(...copySubjectIdList.filter((i) => selfSubjectIds.includes(i.value)))
        } else {
          subjectIdList.splice(0, subjectIdList.length)
        }
      } else {
        subjectIdList.splice(0, subjectIdList.length)
      }

      resolve(subjectIdListData)
    })
  })
}

// 获取练习记录
export const getKpointPracticeRecord = (data) => {
  return request.get('/app-api/exam/kpointPractice/getUserKpointPracticePage', data)
}

// 获得练习记录做题列表
export const getKpointPracticeRecordQuestionList = (data) => {
  return new Promise(async (resolve) => {
    const res = await request.post(
      '/app-api/exam/praccticeStudentQuestion/getPracticeQuestionList',
      data
    )

    try {
      if (res.data) {
        // 如果不是学科网题目，将题目改为学科网的题目结构
        res.data.list = res.data.list.map((q) => {
          // 菁优网
          if (
            q.questionDto.courseId != '10086' &&
            q.questionDto.sourceType &&
            q.questionDto.sourceType === '3'
          ) {
            jywConvertToXkw(q.questionDto)
          }

          return q
        })

        // 题目解析
        const { data: questionSplitRes = [] } = await questionSplitApi(
          res.data.list.map((i) => {
            const { questionId, stem, answer, explanation } = i.questionDto

            return {
              questionId,
              stem,
              answer,
              explanation
            }
          })
        )
        // 获取题型
        const { data: questionTypeDict } = await getQuestionTypeListApi({
          ids: res.data.list.map((item) => item.questionDto.typeId).join(',')
        })

        res.data.list = res.data.list.map((q) => {
          const {
            questionDto: { typeId }
          } = q

          const questionSplit = questionSplitRes.find((i) => i.questionId === q.questionId)
          q['questionDto']['question'] = {
            stem: JSON.parse(questionSplit.stem),
            answer: JSON.parse(questionSplit.answer),
            explanation: JSON.parse(questionSplit.explanation)
          }
          const questionType = questionTypeDict.find((q) => q.id == typeId)
          q['questionDto']['typeName'] =
            questionType && questionType.name ? questionType.name : '未知题型'
          q['questionDto']['answerScoreable'] = getAnswerScoreable({
            stem: q.questionDto.question.stem,
            answer: q.questionDto.question.answer
          })
          q.answerScoreable = getAnswerScoreable({
            stem: q.questionDto.question.stem,
            answer: q.questionDto.question.answer
          })

          return q
        })

        resolve(res)
      } else {
        resolve(res)
      }
    } catch (error) {}
  })
}

// 获取题型
export const getQuestionType = (data) => {
  return request.get('/app-api/question/App/type/get', data)
}

// 获取题型列表
export const getQuestionTypeListApi = (data) => {
  return request.get('/app-api/question/App/type/list', data)
}
